package com.hdoit.kylin.web.studentManager.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.studentManager.model.Excel_head;
import com.hdoit.kylin.logical.studentManager.service.IExcel_headService;
import com.hdoit.kylin.logical.viewmodel.Excel_headSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.JsonUtils;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.JsonActionResult;

@Controller
@RequestMapping(value = "/admin/excel_head/")
public class Excel_headController extends BaseController{
	@Override
	public String getRoutePath() {
		return "areas/admin/excel_head/";
	}
	private IExcel_headService excel_headService;
	public IExcel_headService getExcel_headService() {
		return excel_headService;
	}
	
	@Resource
	public void setExcel_headService(IExcel_headService excel_headService) {
		this.excel_headService = excel_headService;
	}

	private String key1 = "";
	//Excel_headSearch key =new Excel_headSearch();
	private final String indexViewName = "index";
	private final String saveViewName = "save";
	private final String dictColumnJsonViewName = "dictColumnJson";
	
	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,Excel_headSearch search,String key){
    	key1 = key;
    	model.addAttribute("key1", key1);
    	model.addAttribute("search", search);
    	
        return view(indexViewName);
    }
	
	@ResponseBody
    @RequestMapping(value = dictColumnJsonViewName, method = { RequestMethod.POST},produces = "text/json;charset=UTF-8")
    public String dictColumnJson(Excel_headSearch search){
		System.out.println(search.getExcelId()+"2222222222");
    	PagerModel pagerModel=new PagerModel(1000,1);
    	search.setDeleteMark(YesOrNoInteger.no);
    	
    	List<Excel_head> dictColumns= excel_headService.queryList(search,pagerModel);
    	
    	return JsonUtils.writeValueAsString(dictColumns);
    }
	
	  @ResponseBody
	    @RequestMapping(value = saveViewName, method = { RequestMethod.POST},produces = "text/json;charset=UTF-8")
	    public String save(@RequestBody List<Excel_head> excel_heads,String excelId){
	    	//int index=0;
	    	for (Excel_head excel_head : excel_heads) {
	    		excel_head.setExcelId(excelId);
	    		excel_head.setDeleteMark(YesOrNoInteger.no);
	    		//excel_head.setSortCode(index*5);
	    		excel_head.setPropertyId(CommonUtils.getKeyId());
	    		excel_head.setId(CommonUtils.getKeyId());
	    		//excel_head.setCreateDate(DateUtils.getCurrent());
	    		//excel_head.setCreateUserId(getLoginModel().getLoginId());
	    		//excel_head.setCreateUserName(getLoginModel().getLoginName());
	    		
	    		//index++;
			}
	    	
	    	if(excel_headService.saveExcel_heads(excelId, excel_heads)){
	    		return new JsonActionResult(ManageContainer.SUCCESS).toJson();
	    	}else{
	    		return new JsonActionResult(ManageContainer.ERROR).toJson();
	    	}
	    }
}
